package chapter_09.stack;

public class DynStack implements IntStack {
	private int stck[];
	private int tos;
	
	// allocate and initialize stack
	DynStack(int size) {
		stck = new int[size];
		tos = -1;
	}
	
	// Push an item onto the stack
	public void push(int item) {
		// if stack is full, allocate a larger stack
		if (tos == stck.length - 1) {
			int temp[] = new int[stck.length * 2]; // double size
			for (int i = 0; i < stck.length; i++)
				temp[i] = stck[i];
			stck = temp;
			stck[++tos] = item;
		}
		else
			stck[++tos] = item;
	}
	
	public int pop() {
		if (tos < 0) {
			System.out.println("Stack underflow.");
			return 0;
		}
		return stck[tos--];
	}
	
	public static void main(String[] args) {
		IntStack mystack; // create an interface reference variable
		DynStack ds = new DynStack(5);
		FixedStack fs = new FixedStack(8);
		mystack = ds; // load dynamic stack
		// push some numbers onto the stack
		for(int i=0; i<12; i++) mystack.push(i);
		mystack = fs; // load fixed stack
		for(int i=0; i<8; i++) mystack.push(i);
		mystack = ds;
		System.out.println("Values in dynamic stack:");
		for(int i=0; i<12; i++)
			System.out.println(mystack.pop());
		mystack = fs;
		System.out.println("Values in fixed stack:");
		for(int i=0; i<8; i++)
			System.out.println(mystack.pop());
		/*
		mystack是IntStack接口的一个引用。因此，当它引用ds时，它使用DynStack
		实现所定义的push( )和pop( )方法。当它引用fs时，它使用FixedStack定义的
		push( )和pop( )方法。已经解释过，这些决定是在运行时做出的。通过接口引
		用变量获得接口的多重实现是Java完成运行时多态的最有力的方法。
		 */
	}
}
